package com.example.plugin_native.createFarm

import com.map.interfaces.MapZoomCallBack
import com.map.interfaces.Point
import com.map.tiandimap.MapView
import com.map.utils.MapUtils
import java.util.*

class MapControl(private val mapView: MapView) {
    /**
     * 地图画线的点集合
     */
    val linePoints = LinkedList<Point>()

    /**
     * 被撤销的线保存
     */
    val registerPoints = LinkedList<Point>()

    /**
     * 地块的tag
     */
    val newFarmTag = "newFarmTag"

    /**
     * 画线
     */
    fun drawLine(lng: Double, lat: Double, onClose: () -> Unit = {}) {
        mapView.addMarker(lng, lat)
        //从新绘制清除撤销的点
        if (registerPoints.size > 0) {
            registerPoints.clear()
        }
        linePoints.addLast(Point(lng, lat))
        val pointsNum = linePoints.size
        if (pointsNum > 1) {
            var distance: Int
            mapView.getZoom(object : MapZoomCallBack {
                override fun zoom(zoom: Int) {
                    //根据地图缩放界别，动态判断起始点的点击范围
                    distance = when (zoom) {
                        in 0..6 -> 200
                        in 6..8 -> 150
                        in 8..10 -> 100
                        in 10..12 -> 80
                        in 12..16 -> 40
                        in 16..20 -> 20
                        else -> 10
                    }
                    //如果与起点的距离小于10m,则闭合区域
                    if (MapUtils.getDistance(linePoints[pointsNum - 1], linePoints[0]) < distance
                        && pointsNum > 3
                    ) {
                        mapView.clearAllLayer()
                        linePoints.removeLast()
                        drawArea(linePoints, tag = newFarmTag)
                        onClose()
                    } else {
                        mapView.drawLine(
                            points = listOf(linePoints[pointsNum - 2], linePoints[pointsNum - 1]),
                            color = "#FFFFFF"
                        )
                    }
                }
            })
        }
    }

    /**
     * 撤销
     */
    fun revocation() {
        //画的点已被全部撤销
        if (linePoints.size == 0) {
            return
        }
        mapView.clearAllLayer()
        registerPoints.addLast(linePoints.last)
        linePoints.removeLast()
        //画点
        linePoints.forEach {
            mapView.addMarker(it.lng, it.lat)
        }
        //画线
        val pointsNum = linePoints.size
        if (pointsNum > 1) {
            mapView.drawLine(
                points = linePoints,
                color = "#FFFFFF"
            )
        }
    }

    /**
     * 恢复
     */
    fun recover() {
        if (registerPoints.size == 0) {
            return
        }
        mapView.clearAllLayer()

        linePoints.addLast(registerPoints.last)
        registerPoints.removeLast()
        //画点
        linePoints.forEach {
            mapView.addMarker(it.lng, it.lat)
        }
        val pointsNum = linePoints.size
        if (pointsNum > 1) {
            mapView.drawLine(
                points = linePoints,
                color = "#FFFFFF"
            )
        }
    }

    /**
     * 画平面图
     */
    fun drawArea(
        points: List<Point>,
        color: String = "#FFFFFF",
        opacity: Float = 0.8f,
        fillColor: String = "#229B60",
        fillOpacity: Float = 0.5f,
        isEdit: Boolean = true,
        tag: String = "polygon"
    ) {
        if (points.size < 3) {
            return
        }
        mapView.drawArea(
            points,
            color = color,
            opacity = opacity,
            fillColor = fillColor,
            fillOpacity = fillOpacity,
            isEdit = isEdit,
            tag = tag
        )
    }
}